package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"morleyStoneHome/service"
	"morleyStoneHome/util"
	"net/http"
	"strconv"
)

type NewsController struct {
}

func NewNewsController() *NewsController {
	controller := &NewsController{}
	return controller
}

func (c *NewsController) NewsList(ctx *gin.Context) {
	fmt.Println("===========> get request")
	language := ctx.PostForm("language")
	fmt.Println("language = ", language)
	if "" == language {
		language = "zh"
	}
	all, err := service.NewsService.ListAll(language)
	if nil != err {
		ctx.String(http.StatusBadRequest, util.JsonData(err))
	}
	ctx.String(http.StatusOK, fmt.Sprintf("%s", util.JsonData(all))) //获取到post请求参数
}

func (c *NewsController) News(ctx *gin.Context) {
	newsId := ctx.Param("newsId")
	lan := ctx.PostForm("language")
	atoi, err := strconv.Atoi(newsId)
	if nil != err {
		logrus.Error(err)
		ctx.String(http.StatusBadRequest, fmt.Sprintf(util.JsonData(err)))
	}
	one, err := service.NewsService.FindOne(atoi, lan)
	if nil != err {
		ctx.String(http.StatusBadRequest, fmt.Sprintf(util.JsonData(err)))
	}
	data := util.JsonData(one)
	ctx.String(http.StatusOK, fmt.Sprintf("%s", data))
	//resultRes := result.NewResult(ctx)//标准的result，但需要前端处理转义符 \n \r 等
	//news := one[0]
	//resultRes.Success(news)
}
